package com.android.readcard.read;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.android.readcard.bean.CardData;
import com.android.readcard.bean.ReadCardInfo;
import com.android.readcard.inter.UpLoadListener;
import com.android.readcard.utils.BleGlobUtils;
import com.android.readcard.utils.UploadCardUtils;

import java.io.UnsupportedEncodingException;

import cc.lotuscard.LotusCardDriver;
import cc.lotuscard.TwoIdInfoParam;

/**
 * Created by yucheng on 2017/12/4.
 */

public class OtgMiNiUtils {

    //    private final String url = "idcard.djlm.wang";
    private String url = "60.13.188.194";
    //        private final String url = "220.171.49.37";
    private LotusCardDriver mLotusCardDriver;
    private int m_nDeviceHandle = -1;

    // 注册成功失败监听
    private UpLoadListener mOnUpLoadListener;

    private String bluetoothName = "";
    private Bitmap photo;
    private String bluetoothType;

    public OtgMiNiUtils(String url, String bluetoothType, LotusCardDriver mLotusCardDriver,
                        final String bluetoothName, final UpLoadListener mOnUpLoadListener) {
        this.url = url;
        this.bluetoothType = bluetoothType;
        this.mLotusCardDriver = mLotusCardDriver;
        this.bluetoothName = bluetoothName;
        this.mOnUpLoadListener = mOnUpLoadListener;
    }

    // OTJ迷你器读卡.....
    public void OnTestIdListener() {
        if (null == mLotusCardDriver) {
            setErr("打开设备失败,mLotusCardDriver为空");
            return;
        }
        if (m_nDeviceHandle == -1) {

            m_nDeviceHandle = mLotusCardDriver.OpenDevice(url, 0, 0, 0, 0,// 使用内部默认超时设置
                    true);
            Log.e("OTG读卡", "------m_nDeviceHandle------" + m_nDeviceHandle);
        }
        if (m_nDeviceHandle != -1) {
            try {
                testId(url, m_nDeviceHandle);
            } catch (Exception e) {
                setErr("读卡失败");
                e.printStackTrace();
            }
        } else {
            setErr("打开设备失败,m_nDeviceHandle=" + m_nDeviceHandle);
        }
    }

    private void setErr(String err) {
        mOnUpLoadListener.onErrUpLoad(9, err);
    }

    private String name, sex, nation, id_num, address, sign_office, to, from, born;
    private String birth_year, birth_month, birth_day, sign_data, photoHeard;

    //OTJ迷你器
    private void testId(String strServerIp, long nDeviceHandle) throws UnsupportedEncodingException {
        boolean bResult = false;
        String temp = null;
        TwoIdInfoParam tTwoIdInfo = new TwoIdInfoParam();
        bResult = mLotusCardDriver.Beep(nDeviceHandle, 10);
        if (!bResult) { // 读卡错误
            setErr("读卡失败Beep");
            return;
        }
        bResult = mLotusCardDriver.SetCardType(nDeviceHandle, 'B');
        if (!bResult) { // 读卡错误
            setErr("读卡失败SetCardType");
            return;
        }
        bResult = mLotusCardDriver.GetTwoIdInfoByServer(nDeviceHandle, strServerIp, tTwoIdInfo);
        if (!bResult) { // 读卡错误
            setErr("读卡失败GetTwoIdInfoByServer");
            return;
        }
        //姓名
        name = new String(tTwoIdInfo.arrTwoIdName, 0, 30, "UTF-16LE").trim();
        //性别
        temp = new String(tTwoIdInfo.arrTwoIdSex, 0, 2, "UTF-16LE").trim();
        if (temp.equals("1")) {
            sex = "男";
        } else {
            sex = "女";
        }
        //民族
        temp = new String(tTwoIdInfo.arrTwoIdNation, 0, 4, "UTF-16LE").trim();
        try {
            int code = Integer.parseInt(temp.toString());
            nation = decodeNation(code);
        } catch (Exception e) {
            temp = "";
        }
        //出生日期
        born = new String(tTwoIdInfo.arrTwoIdBirthday, 0, 16, "UTF-16LE").trim();
        birth_year = born.substring(0, 4);
        birth_month = born.substring(4, 6);
        birth_day = born.substring(6, 8);
        //住址
        address = new String(tTwoIdInfo.arrTwoIdAddress, 0, 70, "UTF-16LE").trim();
        //身份证号码
        id_num = new String(tTwoIdInfo.arrTwoIdNo, 0, 36, "UTF-16LE").trim();
        //签发机关
        sign_office = new String(tTwoIdInfo.arrTwoIdSignedDepartment, 0, 30, "UTF-16LE").trim();
        //有效期起始日期
        from = new String(tTwoIdInfo.arrTwoIdValidityPeriodBegin, 0, 16, "UTF-16LE").trim();
        //有效期截止日期 UNICODE YYYYMMDD 有效期为长期时存储“长期”
        to = new String(tTwoIdInfo.arrTwoIdValidityPeriodEnd, 0, 16, "UTF-16LE").trim();


        //19921117
        if (to.contains("长期")) {
            sign_data = from.substring(0, 4)
                    + "." + from.substring(4, 6) + "." +
                    from.substring(6, 8) + "--" +
                    "长期";
        } else {
            sign_data = from.substring(0, 4)
                    + "." + from.substring(4, 6) + "." +
                    from.substring(6, 8) + "--" +
                    to.substring(0, 4) + "."
                    + to.substring(4, 6) + "." +
                    to.substring(6, 8);
        }

        if (tTwoIdInfo.unTwoIdPhotoJpegLength > 0) {
            photo = BitmapFactory.decodeByteArray(tTwoIdInfo.arrTwoIdPhotoJpeg, 0,
                    tTwoIdInfo.unTwoIdPhotoJpegLength);
            photoHeard = BleGlobUtils.replaceBlank(
                    BleGlobUtils.bitmapToBase64(photo));

        }
        String chuanMa = UploadCardUtils.getChuanMa("", "");// 获取串码
// 获取身份证数据
        ReadCardInfo cardInfo = UploadCardUtils.getCardInfo(bluetoothName, address, photoHeard, sex, id_num, nation,
                sign_office, to, name, from, born, sign_data, birth_year,
                birth_month, birth_day, photo);

        // 获取读卡上传数据,使用时需要把data转换成json和串码一起上传
        CardData cardData = UploadCardUtils.getData(bluetoothType, address, photoHeard, sex, id_num, nation,
                sign_office, to, name, from, born, sign_data, birth_year,
                birth_month, birth_day, photo);

        mOnUpLoadListener.onSucUpLoad(cardInfo, cardData, chuanMa);

    }

    private String decodeNation(int code) {
        String nation;
        switch (code) {
            case 1:
                nation = "汉";
                break;
            case 2:
                nation = "蒙古";
                break;
            case 3:
                nation = "回";
                break;
            case 4:
                nation = "藏";
                break;
            case 5:
                nation = "维吾尔族";
                break;
            case 6:
                nation = "苗";
                break;
            case 7:
                nation = "彝";
                break;
            case 8:
                nation = "壮";
                break;
            case 9:
                nation = "布依";
                break;
            case 10:
                nation = "朝鲜";
                break;
            case 11:
                nation = "满";
                break;
            case 12:
                nation = "侗";
                break;
            case 13:
                nation = "瑶";
                break;
            case 14:
                nation = "白";
                break;
            case 15:
                nation = "土家";
                break;
            case 16:
                nation = "哈尼";
                break;
            case 17:
                nation = "哈萨克";
                break;
            case 18:
                nation = "傣";
                break;
            case 19:
                nation = "黎";
                break;
            case 20:
                nation = "傈僳";
                break;
            case 21:
                nation = "佤";
                break;
            case 22:
                nation = "畲";
                break;
            case 23:
                nation = "高山";
                break;
            case 24:
                nation = "拉祜";
                break;
            case 25:
                nation = "水";
                break;
            case 26:
                nation = "东乡";
                break;
            case 27:
                nation = "纳西";
                break;
            case 28:
                nation = "景颇";
                break;
            case 29:
                nation = "柯尔克孜";
                break;
            case 30:
                nation = "土";
                break;
            case 31:
                nation = "达斡尔";
                break;
            case 32:
                nation = "仫佬";
                break;
            case 33:
                nation = "羌";
                break;
            case 34:
                nation = "布朗";
                break;
            case 35:
                nation = "撒拉";
                break;
            case 36:
                nation = "毛南";
                break;
            case 37:
                nation = "仡佬";
                break;
            case 38:
                nation = "锡伯";
                break;
            case 39:
                nation = "阿昌";
                break;
            case 40:
                nation = "普米";
                break;
            case 41:
                nation = "塔吉克";
                break;
            case 42:
                nation = "怒";
                break;
            case 43:
                nation = "乌孜别克族";
                break;
            case 44:
                nation = "俄罗斯";
                break;
            case 45:
                nation = "鄂温克";
                break;
            case 46:
                nation = "德昂";
                break;
            case 47:
                nation = "保安";
                break;
            case 48:
                nation = "裕固";
                break;
            case 49:
                nation = "京";
                break;
            case 50:
                nation = "塔塔尔";
                break;
            case 51:
                nation = "独龙";
                break;
            case 52:
                nation = "鄂伦春";
                break;
            case 53:
                nation = "赫哲";
                break;
            case 54:
                nation = "门巴";
                break;
            case 55:
                nation = "珞巴";
                break;
            case 56:
                nation = "基诺";
                break;
            case 97:
                nation = "其他";
                break;
            case 98:
                nation = "外国血统中国籍人士";
                break;
            default:
                nation = "";
                break;
        }
        return nation;
    }
}
